clear all
set more off
cd "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"


*2003-2014
use  "S:\Project\DemoSos2\data2020\FDtrygd\arb\f_jobber.dta", clear
drop if  lopenr == "0" | lopenr == "" 
destring kode, replace
label def kode 0 "Avgang (Depature)" 1 "Tilgang (Arrival)" ///
	2 "Endring (Change ex promotion)" 3 "Left censured" 9 "Left censured; uknown"
label value kode kode

egen lopenr_jobb_num = group(lopenr_jobb) //num job id, so shorter
bysort lopenr_jobb_num: egen lopenr_jobb_occurence = count(lopenr) //rows per job
gen departure = (tomdato!=.) // determine if job ever ends
sort lopenr tilgdato lopenr_jobb_num regdato //sort to put all job events in order
gen start_date = regdato if kode!=0 //spell start date is regdata (within job)
gen end_date = . if kode!=0 //no end date if job doesn't end
replace end_date = regdato[_n+1] if kode!=0 & lopenr==lopenr[_n+1] //end date is next spell start date
format start_date %td
format end_date %td
drop lopenr_jobb a_org_ord ant_rec b_dager ant_recdato b_dagerdato gjsn_arbtimdato
drop if kode==1
save "_temp.dta", replace

forvalues i = 2003/2014     {
	use "_temp.dta", clear
	local j = `i'+1
	* hours for each job in year i
	gen hours = gjsn_arbtim if start_date < d(01jan`i') ///start before, cont into
		& end_date >= d(01jan`i') 
	replace hours = gjsn_arbtim if start_date >= d(01jan`i') ///start during
		& start_date < d(01jan`j')  //start during
	
	ge morethan35 = hours >= 3500 if hours!=. //7July2023 these use to say gjsn_arbtim instead of hours
	ge morethan37 = hours >= 3700 if hours!=.
	ge morethan3750 = hours >= 3750 if hours!=. //added greater than or equal
	ge morethan40 = hours>= 4000 if hours!=.
	
	replace hours = hours/100
	
	collapse hours morethan3750 morethan40 (max) m_morethan35=morethan35 m_morethan37=morethan37 ///
		m_morethan40=morethan40 m_morethan3750=morethan3750, by(lopenr) 
	gen year = `i'   
	save "_temp`i'", replace
}

use "_temp2003", clear
for num 2004/2014: append using "_tempX"
save "hours2003_2014", replace

erase "_temp.dta"
for num 2003/2014: erase "_tempX.dta"

*2015-2021	
foreach i of numlist 2015/2021 {
	foreach k of numlist 1/12 {
		use lopenr lonn_uregtil_arbeidet lonn_uregtil_uarbeidet lonn_bonus lonn_overtid lonn_overtid_timer ///
			arb_arbeidstid  arb_heldeltid arb_aarsverk arb_timeant_fulltid ///
			using "S:\Project\DemoSos2\data2020\SSB2022\FDtrygd\arb\aordn\aord_`i'_m`k'.dta"
		ge month = `k'
		ge year = `i'
		rename arb_arbeidstid hours
		ge partime = arb_heldeltid == 2 if arb_heldeltid!=.
		ge fulltime = arb_heldeltid == 1 if arb_heldeltid!=.
		
		ge morethan35 = hours >= 35.00 if hours!=. 
		ge morethan37 = hours >= 37.00 if hours!=. 
		ge morethan3750 = hours >= 37.50 if hours!=. //added greater than or equal
		ge morethan40 = hours>= 40.00 if hours!=. //scaled to be consistent with hours
		
		drop arb_heldeltid arb_timeant_fulltid
		save "arby`i'm`k'", replace
	}
	
	use "arby`i'm1", clear
	for num 2/12: append using "arby`i'mX"
	collapse (mean) lonn_uregtil_arbeidet lonn_uregtil_uarbeidet lonn_bonus lonn_overtid lonn_overtid_timer hours arb_aarsverk ///
	(max) partime fulltime max_arb_aarsverk=arb_aarsverk m_morethan35=morethan35 m_morethan37=morethan37 ///
	m_morethan40=morethan40 m_morethan3750=morethan3750 (min) min_arb_aarsverk=arb_aarsverk, by(lopenr)
	ge year = `i'
	save "arby`i'", replace
	for num 1/12: erase "arby`i'mX.dta"
}
	
use "arby2015", clear
for num 2016/2020: append using "arbyX"
save "hours2015_2020", replace

for num 2015/2020: erase "arbyX.dta"

use "hours2003_2014", clear
append using "hours2015_2020"
save "hours", replace
